// by Xeno
//#define __DEBUG__
#define THIS_FILE "fn_handleobservers.sqf"
#include "x_setup.sqf"
private ["_nextaritime", "_height", "_type", "_radius", "_i", "_observer", "_enemy", "_distance", "_man_type"];
if !(call FUNC(checkSHC)) exitWith {};

_enemy_ari_available = true;
_nextaritime = 0;
_height = 150;
_type = "";
_radius = 30;

#ifndef __TT__
_man_type = switch (GVAR(enemy_side)) do {
	case "BLUFOR": {"SoldierWB"};
	case "OPFOR": {"SoldierEB"};
	case "INDEPENDENT": {"SoldierGB"};
};
#else
_man_type = ["SoldierWB","SoldierEB"];
#endif
__TRACE_1("Observers",GVAR(obs_array));
__TRACE_1("Vehicles",GVAR(ai_artiller_unit_vecs));
sleep 10.123;
while {GVAR(nr_observers) > 0 && (GVAR(WithEnemyFireBase) > 0 || (count GVAR(ai_artiller_unit_vecs) > 0))} do {
	GVAR(ai_artiller_unit_vecs) = GVAR(ai_artiller_unit_vecs) - [objNull];
	call FUNC(mpcheck);
	{
		private "_observer";
		_observer = _x;
		if (!isNull _observer && {alive _observer} && {_enemy_ari_available}) then {
			_enemy = _observer findNearestEnemy _observer;
			if (!isNull _enemy && {(_observer knowsAbout _enemy >= 1.5)} && {!((vehicle _enemy) isKindOf "Air")}) then {
				_distance = _observer distance _enemy;
				__TRACE_1("Found enemy",_enemy);
				if (_distance < 500) then {
					_pos_nearest = [getPosASL _enemy select 0, getPosASL _enemy select 1, 0];
					_near_targets = _pos_nearest nearEntities [_man_type, 35];
					_enemy_ari_available = false;
					_nextaritime = time + GVAR(ai_arti_available_time) + (random 120);
					if (count _near_targets == 0) then {
						if (count GVAR(ai_artiller_unit_vecs) > 0) then {
							[GVAR(ai_artiller_unit_vecs),_pos_nearest,floor (random 2)] spawn FUNC(shootarivec);
						} else {
							[_pos_nearest,floor (random 2)] spawn FUNC(shootari);
						};
					} else {
						if ((random 100) < 15) then {// 1 to 6 chance for smoke
							if (count GVAR(ai_artiller_unit_vecs) > 0) then {
								[GVAR(ai_artiller_unit_vecs),_pos_nearest,2] spawn FUNC(shootarivec);
							} else {
								[_pos_nearest,2] spawn FUNC(shootari);
							};
						};
					};
				};
			};
		};
		sleep 2.321;
	} forEach GVAR(obs_array);
	sleep 5.123;
	if (time > _nextaritime && {!_enemy_ari_available}) then {_enemy_ari_available = true};
};
